package leetCode.firstWeek;

public class Code_23 {
  public ListNode mergeKLists(ListNode[] lists) {
    ListNode tmp = new ListNode(0);
    ListNode res = tmp;
    while (true) {
      int i, change = 0, point = 0, MIN = Integer.MAX_VALUE;
      for (i = 0; i < lists.length; i++) {
        if (lists[i] != null && lists[i].val < MIN) {
          MIN = lists[i].val;
          point = i;
          change = 1;
        }
      }
      if (change == 0) {
        break;
      }
      tmp.next = new ListNode(lists[point].val);
      lists[point] = lists[point].next;
      tmp = tmp.next;
    }
    return res.next;
  }
}
